package com.ztgf.shopping.api.cms.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.shopping.models.bo.shop.ShopMemberRelationBO;
import com.ztgf.shopping.models.bo.shop.ShopMemberRelationSaveBO;
import com.ztgf.shopping.models.dto.shop.ShopMemberRelationDTO;
import com.ztgf.shopping.models.service.shop.IShopMemberRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 店铺会员表后台接口类
 *
 * @author zhanghualang
 * @Date 2020-05-26
 */

@RestController
@RequestMapping("/cms/shopMemberRelation")
@Slf4j
@Api(value = "ShopMemberRelationController", tags = "店铺会员表接口")
public class ShopMemberRelationController {
    @DubboReference
    private IShopMemberRelationService IShopMemberRelationService;

    @ApiOperation(value = "店铺会员表列表", notes = "店铺会员表列表")
    @PostMapping("/queryPageList")
    public Result<Page<ShopMemberRelationDTO>> queryPageList(ShopMemberRelationBO shopMemberRelationBO) {
        Wrapper<ShopMemberRelationDTO> wrapper = new EntityWrapper<>();

        if (shopMemberRelationBO.getSellShopId() != null) {
            wrapper.eq("sell_shop_id", shopMemberRelationBO.getSellShopId());
        }

        if (shopMemberRelationBO.getBuyShopId() != null) {
            wrapper.eq("buy_shop_id", shopMemberRelationBO.getBuyShopId());
        }

        wrapper.orderBy("create_time", false);//降序
        Page<ShopMemberRelationDTO> page = new Page<>(shopMemberRelationBO.getPageNo(), shopMemberRelationBO.getPageSize());
        page = IShopMemberRelationService.selectPage(page, wrapper);
        return new Result<>(page);
    }

    @ApiOperation(value = "根据双方店铺ID，获取会员信息", notes = "根据双方店铺ID，获取会员信息")
    @PostMapping("/getMemberById")
    public Result<ShopMemberRelationDTO> getMemberById(ShopMemberRelationBO bo) {
        if (bo.getBuyShopId() == null || bo.getSellShopId() == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        Wrapper<ShopMemberRelationDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("sell_shop_id", bo.getSellShopId());
        wrapper.eq("buy_shop_id", bo.getBuyShopId());
        ShopMemberRelationDTO dto = IShopMemberRelationService.selectOne(wrapper);
        return new Result<>(dto);
    }
}
